package com.zzyl.common.utils.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.core.auth.Auth;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 千帆调用接口
 *
 * @author: JBY
 * @Date: 2025/08/17 8:48
 */
@Component
@Slf4j
public class BaiDuInvoker {

    @Autowired
    private BaiduAIProperties baiduAIProperties;

    public String QianfanInvoker(String prompt) {

        // 使用安全认证AK/SK鉴权，替换下列示例中参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk，如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
        Qianfan qianfan = new Qianfan(baiduAIProperties.getAccessKey(), baiduAIProperties.getSecretKey());

        // 指定模型
        ChatResponse response = qianfan.chatCompletion()
                .model(baiduAIProperties.getQianfanModel())
                .addMessage("user", prompt)
                .maxOutputTokens(2048)
                .temperature(0.7)
                .responseFormat("json_object")
                .execute();

        return response.getResult();
    }
}
